<?php

namespace frontend\controllers;

use bc\controller\FrontController;
use bc\wx\Auth;
use bc\util\Util;
use yii\web\Session;
use bc\wx\OAuth;
use bc\ar\MActiveRecord;
use bc\util\Sms;
use bc\service\SpokesmanService;
use bc\controller\WxOAuthController;
use yii\db\Query;
use bc\controller\BackendController;
use bc\model\product\detail\BounsModel;
use bc\service\gene\FpService;
use bc\util\ImageMergeQrcode;
use bc\model\product\DistributionModel;
use bc\model\product\QrcodeModel;
use bc\model\order\report\ShareModel;
use bc\util\Math;
use bc\service\CouponService;
use bc\model\coupons\CouponsModel;
use bc\entity\Order;
use bc\entity\Coupon;

class TestController extends FrontController
{
	
	public function actionRedis()
	{
		
		echo $this->getBaseUrl();
		
		try {
			$redis = new \Redis();
			$redis->connect('139.196.104.113', 6380);
			$redis->auth('NR98G7r8');
			
			
			$redis->set("time", date('Y-m-d'));
			echo $redis->get("time");
			
		}
		catch (\Exception $e)
		{
			echo $e->getMessage();
		}
	}
	
	public function actionIndex()
	{
		// 生成js sdk签名
		$jsConfig = $this->configJsApi ();
		$appid = $this->wechat ()->appid;
		$secret = $this->wechat ()->secret;
		$view ['share_url'] = 'http://wx.zhishimama.com';
		$view ['share_content'] = '默认信息';
		$view ['share_desc'] = '分享';
		$view ['js_config'] = $jsConfig;
		
		
		return $this->render('index', $view);
	}
	
	
	
	
	public function actionAuth()
	{
		$session = new Session();
		$user = $session->get('user');
		if(!$user)
			$this->setOAuth(OAuth::SNSAPI_USERINFO);
			
		if($user['openid'])
		{
			$dao = new MActiveRecord();
			$sql = 'select * from ecs_users where wxid = "' . $user['openid'] . '"';
			$result = $dao->getDb()->createCommand($sql)->queryOne();
			print_r($result['user_id']);
		}
			
	}
	
	public function actionUser()
	{
		$session = new Session();
		print_r($session->get('shop_user'));
	}
	
	public function actionTest()
	{
		$view ['share_url'] = 'http://wx.zhishimama.com';
		$view ['share_content'] = '动态的内容';
		$view ['share_desc'] = '动态分享的';
		echo json_encode($view);
		exit;
	}
	
	
	public function actionA()
	{
		$model = new CouponsModel();
		
		print_r($model->getUser());
		
		$rs = CouponsModel::find()->joinWith('bc_coupons_user')->limit(10)->all();
		echo '<pre>';
		print_r($rs);
	}
	
	public function actionCoupon()
	{
		$order = new Order();
		$coupon = new Coupon();
		
		$order->user_id = 4279;
		$order->group_id = 0;
		$order->order_id = 1000;
		$order->price = 120;
		$order->product_id = 2;
		
		
		$coupon->coupon_id = 9;
		$coupon->coupons_money = 20;
		$coupon->coupons_use_target = 1;
		$coupon->coupons_type = 1;
		$coupon->product_group_id = 0;
		$coupon->product_id = 2;
		
		$service = new CouponService();
		
		$service->setCoupon($coupon);
		$service->setOrder($order);
		$price = $service->checkout();
		
		print_r($price);exit;
		
	}
	
	/**
	 * 发送文本消息
	 */
	protected function sendText()
	{
		$xmlTemplate = '<xml>
			<ToUserName><![CDATA[%s]]></ToUserName>
			<FromUserName><![CDATA[%s]]></FromUserName>
			<CreateTime>%s</CreateTime>
			<MsgType><![CDATA[text]]></MsgType>
			<Content><![CDATA[%s]]></Content>
			</xml>';
		
		$result = sprintf($xmlTemplate, $openid, $wxid, $create_time, $content);
		
	}
	
	
	
}

